package com.fancy.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * cookie工具类
 * Created by FancyFong on 11/9/2017.
 */
public class CookieUtil {

    public static Cookie get(HttpServletRequest request, String key){
        Cookie [] allCookies=request.getCookies();
        Map<String,Cookie> readCookieMap = new HashMap<>();
        if(allCookies!=null){
            for (int i =0;i<allCookies.length;i++){
                if(allCookies[i].getName().equals(key)){
                    return allCookies[i];
                }
            }
        }
        return null;
    }



    public static void set(HttpServletResponse response,String key,String value,int maxAge){

        Cookie cookie = new Cookie(key,value) ;
        cookie.setPath("/");
        cookie.setMaxAge(maxAge);
        response.addCookie(cookie) ;
    }


}
